#include "device.h"

void Micro_switch_Init(void)
{
    GPIO_InitTypeDef  GPIO_InitStruct = {0};

    LEFT_UP_PORT_CLK_ENABLE();
	RIGHT_UP_PORT_CLK_ENABLE ();
	LEFT_DOWN_PORT_CLK_ENABLE();
	RIGHT_DOWN_PORT_CLK_ENABLE();
    
    GPIO_InitStruct.Mode  = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull  = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

    GPIO_InitStruct.Pin = LEFT_UP_PIN;
    HAL_GPIO_Init(LEFT_UP_PORT, &GPIO_InitStruct);
	
	GPIO_InitStruct.Pin = RIGHT_UP_PIN;
    HAL_GPIO_Init(RIGHT_UP_PORT, &GPIO_InitStruct);
	
	GPIO_InitStruct.Pin = LEFT_DOWN_PIN;
    HAL_GPIO_Init(LEFT_DOWN_PORT, &GPIO_InitStruct);
	
	GPIO_InitStruct.Pin = RIGHT_DOWN_PIN;
    HAL_GPIO_Init(RIGHT_DOWN_PORT, &GPIO_InitStruct);
	
}

SW_STATE Micro_switch_Read_State(SW_NUM sw_num)
{
    GPIO_PinState state = GPIO_PIN_SET;
   
    switch(sw_num)
    {
        case L_UP:
            state = HAL_GPIO_ReadPin(LEFT_UP_PORT, LEFT_UP_PIN);
            break;

        case R_UP:
            state = HAL_GPIO_ReadPin(RIGHT_UP_PORT, RIGHT_UP_PIN);
            break;

        case L_DOWN:
            state = HAL_GPIO_ReadPin(LEFT_DOWN_PORT, LEFT_DOWN_PIN);        
            break;

        case R_DOWN:
            state = HAL_GPIO_ReadPin(RIGHT_DOWN_PORT, RIGHT_DOWN_PIN);                
            break;
        
        default:
            break;
    }

    if(state == GPIO_PIN_SET)
        return SW_NOT_TOUCH;
    else
        return SW_TOUCH;
}

